<?php

class Article extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array("url", 'form'));
        $this->load->library('my_layout');
        $this->load->library('pagination');
        $this->my_layout->setLayout("layout/frontend");
        $this->load->model('article_model');
        $this->load->model('center_model');
    }

    private $limit = 5;
    private $limitComment = 5;

    function index($ItemNumber = 0) {

        $article = $this->article_model->get_all($this->limit, $ItemNumber);
        $data['article'] = NULL;

        if ($article->num_rows() > 0) {
            $data['article'] = $article->result_array();
        } else {
            $data['message_notfound'] = 'Không tìm thấy tin tức nào';
        }
        //pageination
        $config['base_url'] = base_url() . 'index.php/website/article/index';
        $config['total_rows'] = $this->article_model->num_rows();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = 4;
        $config['num_links'] = 5;
        $config['next_link'] = 'Next »';
        $config['prev_link'] = '« Prev';
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);

        $data['link'] = $this->pagination->create_links();

        //load center menu
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;

        $this->my_layout->view('frontend/article/list_article_view', $data);
        
    }

    function type($id_type, $ItemNumber = 0) {

        $article = $this->article_model->get_by_type($id_type, $this->limit, $ItemNumber);
        $data['article'] = NULL;
        if ($article->num_rows() > 0) {
            $data['article'] = $article->result_array();
        } else {
            $data['message_notfound'] = 'Không tìm thấy tin tức nào';
        }

        //pageination
        $config['base_url'] = base_url() . 'index.php/website/article/type/' . $id_type . '/';
        $config['total_rows'] = $this->article_model->get_by_type($id_type, NULL, NULL)->num_rows();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = 4;
        $config['num_links'] = 5;
        $config['next_link'] = 'Next »';
        $config['prev_link'] = '« Prev';
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);

        $data['link'] = $this->pagination->create_links();

        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = $id_type;

        $this->my_layout->view('frontend/article/list_article_view', $data);
    }

    function id($id, $itemNumber = NULL) {



        $data['activeTab1'] = ($itemNumber == NULL) ? TRUE : FALSE;
        $data['showArticle'] = ($itemNumber == NULL) ? TRUE : FALSE;

        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        $data['article'] = NULL;
        $article = $this->article_model->get_by_id($id);
        if ($article->num_rows() > 0) {
            $data['article'] = $article->row_array();
            //tang view
            $this->article_model->increate_view($data['article']['id_article'], $data['article']['view']);
            $data['article']['view'] = $data['article']['view'] + 1;
        } else {
            $data['message_notfound'] = 'Không tìm thấy tin tức nào';
        }

        //load comment
        $this->load->model('comment_article_model');
        $commentData = $this->comment_article_model->loadByArticle($id, $this->limitComment, $itemNumber);
        $comment['data'] = $commentData->result_array();

        //paging commment
        $config['base_url'] = base_url() . 'index.php/website/article/id/' . $id . '/';
        $config['total_rows'] = $this->comment_article_model->countAll();
        $config['per_page'] = $this->limitComment;
        $config['uri_segment'] = 5;
        $config['num_links'] = 5;
        $config['next_link'] = 'Next »';
        $config['prev_link'] = '« Prev';
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);

        $comment['link'] = $this->pagination->create_links();
        $data['comment'] = $comment;

        // lấy các bài viết khác
        $article_diff = $this->article_model->getArticleDiff($id);
        $data['article_diff'] = $article_diff->result_array();
        //$data['dem'] = $article_diff->num_rows();

        //loadView
        if ($itemNumber == NULL) {
            $this->my_layout->view('frontend/article/article_view', $data);
        } else {
            $this->my_layout->view('frontend/article/article_comment', $data);
        }
    }

    function add_comment() {

        $this->load->library('form_validation');
        $this->load->library('session');
        // Load data of current user
        $session_data = $this->session->userdata('logged_in');

        //set message
        $this->form_validation->set_message('required', "%s không được để trống.");
        $this->form_validation->set_rules('comment', 'Trả lời', 'trim|required|xss_clean');
        if ($this->form_validation->run($this) == TRUE) {
            // Add data to variable '$data'
            $comment = $this->input->post('comment');
            $id_article = $this->input->post('id_article');
            $id_user = $session_data['id'];

            // Call function create_comment in model 
            $this->load->model('comment_article_model');
            $id = $this->comment_article_model->addComment($id_article, $id_user, $comment);
            // Run action show_id to see new comment (refresh)
            $comment_result = $this->comment_article_model->loadById($id)->row_array();

            echo $this->load->view('frontend/article/comment', $comment_result, TRUE);
        }
    }

}
